package main

import (
	"context"
	"fmt"
	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
	"sync"
)

func main() {
	p, err := rocketmq.NewProducer(
		producer.WithNameServer([]string{"127.0.0.1:9876"}),
		producer.WithGroupName("mxshop"))
	if err != nil {
		panic("生成producer失败 ")
	}
	if err = p.Start(); err != nil {
		panic("启动producer失败 ")
	}
	messages := make(chan string, 50)
	var wg sync.WaitGroup

	for i := 0; i < 5; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			res, err := p.SendSync(context.Background(), primitive.NewMessage("2104A", []byte("色狼")))
			if err != nil {
				fmt.Printf("发送失败 : %s\n", err)
			} else {
				fmt.Printf("发送成功 : %s\n", res.String())
			}
			messages <- res.String()
		}()
	}
	<-messages
	wg.Wait()
	if err := p.Shutdown(); err != nil {
		panic("关闭producer失败 ")
	}
	//<-ch
}
